package newsService

import (
	"gin/dao/entity"
	"gin/dao/mapper/news"
	"gin/dao/mapper/news/newsLike"
	"gin/model/body"
	"gin/service/base"
	"strings"
)

// Add 添加动态
func Add(p body.AddNewsParam) {
	news.Add(p)
}

// Update 修改动态
func Update(p body.UpdateNewsParam) {
	news.Update(p)
}

// List 获取动态列表
func List(uid int64, p body.NewsListParam) ([]*entity.UsersNewsDetail, int64) {
	res, count := news.List(p)
	for i, v := range res {
		urls := strings.Split(v.Url, ",")
		newsUrl := make([]string, 0)
		if len(urls) > 0 {
			for _, url := range urls {
				newsUrl = append(newsUrl, base.CompleteImgUrl(url))
			}
		}

		res[i].Urls = newsUrl
		res[i].IsLike = newsLike.IsExist(uid, v.Id)
		res[i].IsLike = newsLike.IsExist(uid, v.Id)
	}

	return res, count
}

// Del 删除动态
func Del(uid, id int64) {
	news.Del(uid, id)
}

func Detail(id, uid int64) entity.UsersNewsDetail {
	detail := entity.UsersNewsDetail{}
	if id != 0 {
		info := news.Detail(id)
		detail.UsersNews = info
		urls := strings.Split(info.Url, ",")
		newsUrl := make([]string, 0)
		for _, url := range urls {
			newsUrl = append(newsUrl, base.CompleteImgUrl(url))
		}
		detail.Urls = newsUrl

		news.UpdateNum(id, 1, true)
	} else {
		info := news.UserNewNews(uid)
		detail.UsersNews = info
		urls := strings.Split(info.Url, ",")
		newsUrl := make([]string, 0)
		for _, url := range urls {
			newsUrl = append(newsUrl, base.CompleteImgUrl(url))
		}
		detail.Urls = newsUrl

	}
	return detail
}
